﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MainScene;
using MindFusion.Diagramming.WinForms;
using MindFusion.Diagramming;

namespace SSShell
{
    public partial class ChangeClassCaseNode : Form
    {
        frmMain formMain;
        DiagramNode Selected_DNode;
        List<string> AllValue = new List<string>();

        public ChangeClassCaseNode(frmMain _formMain, DiagramNode _selDNode, string text)
        {
            InitializeComponent();
            formMain = _formMain;
            Selected_DNode = _selDNode;
            this.Text = text;
        }

        private void ChangeClassCaseNode_Load(object sender, EventArgs e)
        {
            textBox_CurName.Text = ((ShapeNode)Selected_DNode).Text;
            comboBox_NewName.Items.Clear();
            switch (this.Text)
            {
                case "#действие":
                    AllValue = formMain.SS.LstActions;
                    break;
                case "#агент":
                    comboBox_NewName.DropDownStyle = ComboBoxStyle.DropDownList;
                    AllValue.AddRange(formMain.SS.LstAgents);
                    foreach (string item in formMain.SS.LstRecipients)
                        if (!formMain.SS.LstAgents.Contains(item))
                            AllValue.Add(item);
                    break;
                case "#реципиент":
                    comboBox_NewName.DropDownStyle = ComboBoxStyle.DropDownList;
                    AllValue.AddRange(formMain.SS.LstAgents);
                    foreach (string item in formMain.SS.LstRecipients)
                        if (!formMain.SS.LstAgents.Contains(item))
                            AllValue.Add(item);
                    break;
                case "#объект":
                    AllValue = formMain.SS.LstObjs;
                    break;
                case "#локатив":
                    AllValue = formMain.SS.LstLocotivs;
                    break;
                case "#время":
                    AllValue = formMain.SS.LstTimes;
                    break;
                default:
                    break;
            }
            foreach (string item in AllValue)
                comboBox_NewName.Items.Add(item);
        }

        private void Btn_OK_Click(object sender, EventArgs e)
        {
            string new_text = comboBox_NewName.Text.Trim();
            if (new_text == "")
            {
                //если нет значения
                if (MessageBox.Show("Удалить актор падежной рамки?", "Предупреждение",MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    ((ClassCase)Selected_DNode.Id).Name = "";
                    formMain.diagram2.Nodes.Remove(Selected_DNode);
                }
            }
            else
                if (!comboBox_NewName.Items.Contains(new_text))
                {
                    //если что-то новенькое
                    ((ClassCase)Selected_DNode.Id).Name = new_text;
                    ((ShapeNode)Selected_DNode).Text = new_text;
                    ((ShapeNode)Selected_DNode).ResizeToFitText(FitSize.KeepRatio);
                    AllValue.Add(new_text);
                }
                else
                {
                    //если что-то старенькое
                    ShapeNode case_allready = formMain.GetCaseNodeByName(new_text);
                    if (case_allready != null)
                    {
                        DiagramLinkCollection all_this_link = Selected_DNode.GetAllLinks();
                        foreach (DiagramLink item in all_this_link)
                            item.Destination = case_allready;
                        ((ClassCase)Selected_DNode.Id).Name = new_text;
                        formMain.diagram2.Nodes.Remove(Selected_DNode);
                    }
                    else
                    {
                        ((ClassCase)Selected_DNode.Id).Name = new_text;
                        ((ShapeNode)Selected_DNode).Text = new_text;
                        ((ShapeNode)Selected_DNode).ResizeToFitText(FitSize.KeepRatio);
                    }
                }
        }
    }
}
